﻿
namespace Behemoth.SharePoint.Configuration {

    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;

    using Microsoft.SharePoint.Utilities;

    public class SPFeatureXmlTools {

        internal const string XML_DOC_HEAD = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
        internal const string MOSS_NAMESPACE = "http://schemas.microsoft.com/sharepoint/";
        internal const string MOSS_SOAP_NAMESPACE = "http://schemas.microsoft.com/sharepoint/soap/";

        public static string GetTemplateFilePath(string filePath) {
            string fn = SPUtility.GetGenericSetupPath(@"TEMPLATE\") + filePath;
            return fn;
        }
        public static string GetFeatureFilePath(string featureAndFileName) {
            string fn = SPUtility.GetGenericSetupPath(@"TEMPLATE\FEATURES\") + featureAndFileName;
            return fn;
        }

        [Obsolete("If possible use System.Xml.Linq and XElement queries instead.")]
        [SuppressMessage("Microsoft.Design", "CA1041:ProvideObsoleteAttributeMessage", Justification = "This overloaded method has also been marked as obsolete.")]
        public static XmlDocument GetConfigFile(string featureAndFileName) {
            string fn = GetFeatureFilePath(featureAndFileName);
            XmlDocument doc = new XmlDocument();
            doc.Load(fn);
            return CreateCleanXmlDocument(doc.DocumentElement);
        }

        public static XElement XGetConfigFile(string featureAndFileName) {
            string fn = GetFeatureFilePath(featureAndFileName);
            XElement x = XElement.Load(fn);
            return x;
        }

        [Obsolete("If possible use System.Xml.Linq and XElement queries instead.")]
        public static void RemovePrefixes(XmlNode node) {
            node.Prefix = "";
            foreach (XmlNode subNode in node.ChildNodes) {
                RemovePrefixes(subNode);
            }
        }

        /// <summary>
        /// I just get so sick of this crap that comes back from web services
        /// with namespaces on it that make it next to impossible to get the
        /// XPath syntax correct! This method strips those namespaces out.
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        [Obsolete("If possible use System.Xml.Linq and XElement queries instead.")]
        public static XmlDocument CreateCleanXmlDocument(XmlNode xml, List<string> nameSpacesToRemove) {
            XmlDocument xmlDoc = new XmlDocument();
            string strippedXml = xml.OuterXml;
            foreach (string nameSpaceToRemove in nameSpacesToRemove) {
                strippedXml = strippedXml.Replace("xmlns=\"" + nameSpacesToRemove + "\"", string.Empty);
            }
            // end get...
            xmlDoc.LoadXml(XML_DOC_HEAD + strippedXml);
            return xmlDoc;
        }
        [Obsolete("If possible use System.Xml.Linq and XElement queries instead.")]
        [SuppressMessage("Microsoft.Design", "CA1041:ProvideObsoleteAttributeMessage", Justification = "This overloaded method has also been marked as obsolete.")]
        public static XmlDocument CreateCleanXmlDocument(XmlNode xml) {
            List<string> nameSpacesToRemove = new List<string>();
            nameSpacesToRemove.Add(MOSS_SOAP_NAMESPACE);
            nameSpacesToRemove.Add(MOSS_NAMESPACE);
            return SPFeatureXmlTools.CreateCleanXmlDocument(xml, nameSpacesToRemove);
        }

        /*
        public static string GetFeatureFilePath(SPFeatureDefinition featureDefinition, string featureFile) {
            string sharepointFeaturesDir = GetSharePointFeaturesDirectory();
            string filePath = String.Empty;
            if (featureDefinition != null && !String.IsNullOrEmpty(sharepointFeaturesDir)) {
                string featureName = featureDefinition.DisplayName;
                string featureDir = Path.Combine(sharepointFeaturesDir, featureName);
                filePath = Path.Combine(featureDir, featureFile); // "SiteProvisioning.xml"
            }
            return filePath;
        }

        public static string GetFeatureFilePath(string featureFile, SPFeatureDefinition featureDefinition) {
            string sharepointFeaturesDir = GetSharePointFeaturesDirectory();
            string filePath = String.Empty;
            if (featureDefinition != null && !String.IsNullOrEmpty(sharepointFeaturesDir)) {
                string featureName = featureDefinition.DisplayName;
                string featureDir = Path.Combine(sharepointFeaturesDir, featureName);
                filePath = Path.Combine(featureDir, featureFile); // "SiteProvisioning.xml"
            }
            return filePath;
        }

        public static string GetSharePointTemplateDirectory() {
            string key = @"SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0";
            string name = "Location";
            string dir = String.Empty;
            try {
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey(key);
                string value = regKey.GetValue(name) as string;
                regKey.Close();
                dir = Path.Combine(value, @"template");
            } catch (SecurityException) {
                dir = String.Empty;
            } catch (ArgumentNullException) {
                dir = String.Empty;
            } catch (ArgumentException) {
                dir = String.Empty;
            } catch (ObjectDisposedException) {
                dir = String.Empty;
            } catch (IOException) {
                dir = String.Empty;
            } catch (UnauthorizedAccessException) {
                dir = String.Empty;
            }
            return dir;
        }

        internal static string GetSharePointFeaturesDirectory() {
            string templateDir = GetSharePointTemplateDirectory();
            if (string.IsNullOrEmpty(templateDir))
                return templateDir;
            templateDir = Path.Combine(templateDir, @"features");
            return templateDir;
        }
         */

    } // class

} // namespace
